/*
 * Copyright (c) 2012 Universidad Antonio de Nebrija.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Apache License v2.0
 * which accompanies this distribution, and is available at
 * http://www.apache.org/licenses/LICENSE-2.0.html
 * 
 * Contributors:
 *    Universidad Antonio de Nebrija - initial API, implementation and documentation
 */
package com.nebrija.riif.cli;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import java.util.SortedMap;
import java.util.StringTokenizer;
import java.util.TreeMap;

/**
 * RIIF command-line interface
 * @author asanchep
 *
 */
public class RIIFCLI {

	/** Commands sorted by name */
	protected static final SortedMap<String,Command> commands = new TreeMap<String,Command>();
	
	/** default command that will be executed when no other is found. 
	 * Implementation can be changed at runtime. 
	 */
	private static Command defaultCommand = new NotFoundCommand();
	
	static{
		// set all the commands. TODO: Move it to a properties file+reflection  or use spring IoC
		LoadCommand l = new LoadCommand();
		HelpCommand h = new HelpCommand(commands);
		commands.put("load", l);
		commands.put("l", l);
		commands.put("help", h);
		commands.put("h", h);
		commands.put("calc", new CalculateCommand());
		commands.put("describe", new DescribeCommand());
		commands.put("clear", new ClearCommand());
		commands.put("quit", new QuitCommand());
		commands.put("save", new SaveCommand());
		commands.put("env", new EnvCommand());
		commands.put("list", new ListCommand());
		commands.put("time", new TimeCommand());
		commands.put("runtl", new RunTimelineCommand());
		commands.put("stoptl", new StopTimelineCommand());
		commands.put("step", new StepTimelineCommand());
		commands.put("steprep", new StepRepTimelineCommand());
		commands.put("set", new SetCommand());
		commands.put("trantl", new ForceTransitionTimelineCommand());
	}

	public static void main(String[] args) {
		
		System.out.println ("Welcome to the Java RIIF CLI: Introduce h for help");

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		while(true){
			try {
				System.out.print(">");
				ArrayList<String> params = new ArrayList<String>();
				String commandString = br.readLine();
				StringTokenizer stk = new StringTokenizer(commandString.trim());
				String commandName="";
				if(stk.hasMoreTokens()){
					commandName = stk.nextToken();
				}else{
					continue;
				}
				while(stk.hasMoreTokens()){
					params.add(stk.nextToken());
				}
				Command c = commands.get(commandName);
				if (c==null){
					c = RIIFCLI.getDefaultCommand();
				}
				System.out.println();
				System.out.println(c.execute(params));
			} catch (IOException e) {
				System.exit(1);
			} catch (CommandException e) {
				System.out.println("ERROR:"+e.getMessage());
			}

		}

	}

	public static Command getDefaultCommand() {
		return defaultCommand;
	}

	public static void setDefaultCommand(Command defaultCommand) {
		RIIFCLI.defaultCommand = defaultCommand;
	}

}
